var base = getContextPath();

function getContextPath() {
    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    var result = pathName.substr(0, index + 1);
    return result;
}

//评论天数
var commentDays = $('#commentDays').val();
(function () {
    //查询评论趋势
    $.ajax({
        url : base + '/comment/selectCommentTendency',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#lineChartDiv"));
        },
        success: function (result) {
            lineChartModal(result, 'lineChart', 'month', 'rate', 'type', '', '', '270', "");
        },
        complete: function () {
            removeLoading($("#lineChartDiv"));
        }
    });
    //查询评论占比
    $.ajax({
        url : base + '/comment/selectCommentOccupancy',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#pieChartDiv"));
        },
        success: function (result) {
            var chart2 = pieChartModal(result, "pieChart", "name", "value", 300);
            if (result.length > 0) {
                chart2.on('plotclick', function (ev) {
                    var data = ev.data;
                    if (data) {
                        location.href = base + "/category/categoryRank?flag=2&overviewDay=" +
                            commentDays + "&ocategoryId=11&isbackbtn=1";
                    }
                });
            }
        },
        complete: function () {
            removeLoading($("#pieChartDiv"));
        }
    });
    //查询评论时间分布
    $.ajax({
        url : base + '/comment/selectCommentTimeDistribution',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#pieChart3Div"));
        },
        success: function (result) {
            var chart4 = makeShanModal(result, "pieChart3", 300);
            if (result.length > 0) {
                chart4.on('plotclick', function (ev) {
                    if (ev.data) {
                        openPartialLayer($("#pieChart3Div"));
                        var rangeName = ev.data._origin.time;
                        location.href = base + "/comment/list?flag=1&overviewDay=" + commentDays + "&rangeName=" + rangeName;
                    }
                });
            }
        },
        complete: function () {
            removeLoading($("#pieChart3Div"));
        }
    });
    //查询品类点评数
    $.ajax({
        url : base + '/comment/selectCategoryCommentOccupancy',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#piechart2Div"));
        },
        success:function(result){
            var chart3 =  makePerModal(result,"piechart2",300);
            if(result.length > 0) {
                chart3.on('plotclick', function (ev) {
                    var categoryName = ev.data._origin.category;
                    location.href = base + "/category/categoryRank?flag=1&overviewDay=" + commentDays
                        + '&ocategoryName=' + categoryName + "&isocategory=1&isbackbtn=1";
                });
            }
        },
        complete: function () {
            removeLoading($("#piechart2Div"));
        }
    });
    //查询产品评论类型排名(差评前10)
    $.ajax({
        url : base + '/comment/selectProductCommentTypeRank',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#barChart4Div"));
        },
        success:function(result){
            var chart5 =  horizontalHistogramModal(result, "barChart4", "worst", "num", 300);
            if(result.length > 0){
                chart5.on('plotclick',function(ev) {
                    var productName = ev.data._origin.worst;
                    openPartialLayer($("#barChart4Div"));
                    location.href=base + "/comment/list?flag=1&overviewDay=" + commentDays + "&senseType=0&productName=" + productName;
                });
            }
        },
        complete: function () {
            removeLoading($("#barChart4Div"));
        }
    });
    //查询评论观点排名（消极前10）
    $.ajax({
        url : base + '/comment/selectViewpointRank',
        data: {days:commentDays},
        type: 'GET',
        dataType:'json',
        beforeSend: function () {
            openPartialLayer($("#barChart5Div"));
        },
        success:function(result) {
            horizontalHistogramModal(result, "barChart5", "worst", "num", 300);
            $("#barChart5").removeAttr("style","");
            $("#barChart5").css("height","300");
        },
        complete: function () {
            removeLoading($("#barChart5Div"));
        }
    });
}());
d3.select(self.frameElement).style("height", "1500px");

//切换查询天数
$(".queryTendency").click(function(){
    var attr = $(this).attr("attr");
    $(this).attr("disabled", true);
    window.location.href=base + '/comment/overview?days='+attr;
});